package com.meichis.yndms.repository.manager;

import android.util.ArrayMap;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.yndms.http.CallSubscriber;
import com.meichis.yndms.http.IWebServiceCallback;
import com.meichis.yndms.http.RetrofitUtil.RxSchedulersHelper;
import com.meichis.yndms.model.EventNotification;
import com.meichis.yndms.repository.ApiUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class EventNotificationManager extends BaseManager {

    private static EventNotificationManager INSTANCE;

    public static EventNotificationManager getInstance() {
        if (INSTANCE == null) {
            synchronized (EventNotificationManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new EventNotificationManager();
                }
            }
        }
        return INSTANCE;
    }

    public void getTpmAppSummary(final MutableLiveData<Boolean> showDialog, String partnerId, MutableLiveData<List<EventNotification>> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("partnerId", partnerId);
        getRootApi().getTpmAppSummary(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<EventNotification>>() {
                }.getType(), new IWebServiceCallback<List<EventNotification>>() {
                    @Override
                    public void onSucceed(int sequence, List<EventNotification> resultPack, String ReturnInfo, int Return) {
                        if (resultPack == null || resultPack.size() == 0) {
                            MessageTools.showShortToast("暂无数据！");
                            data.setValue(new ArrayList<>());
                        } else {
                            data.setValue(resultPack);
                        }
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showShortToast(error);
                    }
                }));

    }
}
